package com.myk.game.heroscuffle.skills;

import com.myk.game.heroscuffle.game.Element;
import com.myk.game.heroscuffle.game.HateRecorder;
import com.myk.game.heroscuffle.game.Hero;
import com.myk.game.heroscuffle.game.Skill;
import com.myk.game.heroscuffle.utils.Num2CnUtil;

public class Skill_GuanRi extends Skill {

    private static final int UNLOCK_ROUND = 6; //技能解锁的回合
    private static final int BASE_DAMAGE = 250; //命中伤害值
    private static final int ADD_DAMAGE = 20; //对木系的附加伤害
    private static final int RED_DAMAGE = 20; //对冰系的稀释伤害

    public Skill_GuanRi() {
        super("贯日", "开局第" + Num2CnUtil.convert2cn(UNLOCK_ROUND) + "回合解锁使用次数1次，对指定目标造成大量伤害，木系英雄收到的伤害更高，冰系英雄受到的伤害更低。", 0, 0);
    }

    @Override
    protected void onTrigger() {
        if (hostHero.state.isPlayer) {
            selectTargetHero(0, true);
        }
        else {
            Hero feud = hostHero.state.getAliveFeud();
            if (feud != null) {
                skillEffect(feud);
            }
            else {
                Hero enemy = hostHero.state.getAliveEnemy();
                if (enemy != null) {
                    skillEffect(enemy);
                }
                else {
                    Hero prey = hostHero.state.getAlivePrey();
                    if (prey != null) {
                        skillEffect(prey);
                    }
                    else {
                        Hero other = game.pool.getAnyoneAliveHero(hostHero); //没有目标还能使用技能，other比然不为null
                        skillEffect(other);
                    }
                }
            }
        }
    }

    //玩家选择技能施放目标
    private void selectTargetHero(final int index, final boolean isFirst) {
        game.executor.submit(() -> {
            Hero curSelectHero = game.pool.getAllAliveHeroes().get(index);
            if (curSelectHero == hostHero) {
                int newIndex = index + 1;
                if (newIndex == game.pool.getAliveCount()) {
                    newIndex = 0;
                }
                selectTargetHero(newIndex, false);
            }
            game.ui.setTip("请选" + SKILL_NAME + "施放的目标,当前待选:" + curSelectHero.getObviousName());
            game.ui.resetCtrl();
            game.ui.addCtrl("选中", () -> game.executor.submit(() -> {
                skillEffect(curSelectHero);
                game.executor.resume(); //通知阻塞的线程继续执行以后的代码
            }));
            game.ui.addCtrl("切换", () -> game.executor.submit(() -> {
                int newIndex = index + 1;
                if (newIndex == game.pool.getAliveCount()) {
                    newIndex = 0;
                }
                selectTargetHero(newIndex, false);
            }));
        });
        if (isFirst) { //防止因为多次切换而阻塞多个线程
            game.executor.pause(); //阻塞线程直到接收到通知
        }
    }

    private void skillEffect(Hero target) {
        int damage = BASE_DAMAGE;
        if (target.ELE == Element.WOOD) {
            damage += ADD_DAMAGE;
        }
        else if (target.ELE == Element.ICE) {
            damage -= RED_DAMAGE;
        }
        game.ui.printMsg(target.getObviousName() + "好像要被烧化了一样,");
        target.sufferDamage(null, this, null, damage);
        if (target.state.isAlive) {
            //target.setEnemy(hostHero);
            game.recorder.record(game.round,hostHero,target, HateRecorder.HATE_TYPE_EFFECT);
        }
    }

    @Override
    protected void onUpdate() {
        if (game.round == UNLOCK_ROUND) {
            remnantUsableTimes = 1;
            game.ui.sleepShortly();
            game.ui.printlnMsg(hostHero.getObviousName() + SKILL_NAME + "技能解锁。");
        }
    }

    @Override
    protected void onInit() {

    }
}
